<?php
class omekpi_deliverier_incremental{
    
    function deliveryIncremental($account_id,$d_id){
        $ser_array= array();
        $addarray = array();
        $time = date('Ymd');
        $invoice = &app::get('omekpi')->model('deliverier_invoice');
        $deliverier_log = &app::get('omekpi')->model('deliverier_log');
        $delivery = &app::get('ome')->model('delivery');
        $employee = &app::get('omekpi')->model('employee');
        $dcount = $delivery->count(array('parent_id'=>'0'));
        $filter['employee_account_id'] = $account_id;
        $filter['employee_type'] = 'deliverier';
        $id = $employee->dump($filter);
        if(empty($id)){
            return false;
        }
        $e_id = $id['employee_id']; 
        $addarray['employee_id'] = $e_id;
        $darray['employee_id'] = $e_id;

        //$darray['deliveried_numbers'] = 0;//已发货量可从数库里查，查得到+1更新所有，查不到=1
        $deliveried_numbers=$deliverier_log->getDeliveried_numbers();
        //$deliveried = 1;
        if($deliveried_numbers){
                $deliveried=$deliveried_numbers['deliveried_numbers']+1;
        }
        $del_filter['employee_id'] = $addarray['employee_id'];
        $del_filter['createdate'] = $time;
        $res = $deliverier_log->dump($del_filter);
        
        if($res){
            $addarray['createdate'] = $time;
            $addarray['my_deliveried_numbers'] = $res['my_deliveried_numbers']+1;
            $deliverier_log->updateDeliveried($deliveried);//更新已发货共计个数表
            $deliverier_log->update($addarray);
        }else{
            $darray['createdate'] = $time;
            $darray['delivery_numbers'] = 1000;
            $process = array('process'=>'true','parent_id'=>'0');
            $ans = $delivery->getList('*',$process,0,-1);
            $darray['deliveried_numbers'] = count($ans);
            $darray['my_deliveried_numbers'] = 1;
            $deliverier_log->save($darray);
        }
        $deliverier_log->updateCount($dcount);
        
        $invocie_data = array('employee_id'=>$addarray['employee_id'],'delivery_id'=>$d_id,'createdate'=>date('Ymd'));
        $invoice->save($invocie_data);//更新员工和发货单对应表
    } 
    
}
?>
